/*
 * CsbIndex.h
 *
 *  Created on: Oct 10, 2011
 *      Author: USHonDan
 */

#ifndef CSBINDEX_H_
#define CSBINDEX_H_

#include <queue.h>
#include <stack.h>

#include "IndexNode.h"
#include "LeafNode.h"
#include "Node.h"
#include "../StorageManager/Record.h"

struct stackElement {
	Node* node;
	int index;
};

class CsbIndex {
public:
	CsbIndex(int d);
	~CsbIndex();

	//public methods
	void printKeys();
	void printIndex();
	Record *searchRecord(int searchKey);
	Record *insertRecord(int insertKey, Record *record); //returns NULL if insert failed

private:
	//print helper functions
	void printKeysNode(Node *);
	void printLevel();
	void resetQueue();

	//allocation and split helper functions
	Node *allocateNodeGroup(NodeType type);
	int splitLeafNode(LeafNode *leafToSplit, int nodeGroupIndex, IndexNode *indexAbove);
	int splitIndexNode(IndexNode *indexToSplit, int nodeGroupIndex, IndexNode *indexAbove);

	//insertion helper functions
	Record *insertRecord(IndexNode *currentNode, int insertKey, Record *record);

	//state helper functions
	bool isNodeFull(Node *node);

	//queue/stack navigation helper functions
	void addNodesToQueue(IndexNode *);
	void resetStack();

	const int _d; //node breadth (max number of indices stored in node - _d+1 child nodes in node group
	IndexNode * _root; //root node is initialized as index node
	queue<Node *> _queue; //queue to help in tree print
	stack<stackElement *> _stack; //stack to help navigate up in tree (for splitting on insert)
};

#endif /* CSBINDEX_H_ */
